package com.weight.netty;


import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.weight.common.util.ChannelUtil;
import com.weight.system.command.PO.CommandInfo;
import com.weight.system.command.service.CommandInfoService;
import com.weight.system.equmtgrp.PO.EqumtgrpInfo;
import com.weight.system.equmtgrp.service.EqumtgrpInfoService;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

@Component
public class NettyBooter implements ApplicationListener<ContextRefreshedEvent> 
{
	
	@Autowired
	private CommandInfoService commandInfoService;
	@Autowired
	private EqumtgrpInfoService equmtgrpInfoService;
	
	//指令 指令类型
	public static final Map<String, String> commandMap = new ConcurrentHashMap<String, String>();
	// 注册码  注册码加密串
	public static final Map<String, String> codeMap = new ConcurrentHashMap<String, String>();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if(contextRefreshedEvent.getApplicationContext().getParent()==null){
            try {
                NettyServer.getInstance().start(); 
                
        		//2、查询指令表列表
        		List<CommandInfo> commandInfoList = commandInfoService.findAll();
        
        		for(int i=0;i<commandInfoList.size();i++) { 
        			//指令 指令类型
        			commandMap.put(commandInfoList.get(i).getCommandMesg(), ""+commandInfoList.get(i).getCommandType());
    	   		}

//        		List<EqumtgrpInfo> equmtgrpInfoList = equmtgrpInfoService.findAll();
//        		for(int i=0;i<equmtgrpInfoList.size();i++) { 
//        			// 注册码  注册码加密串
//        			codeMap.put(equmtgrpInfoList.get(i).getEqumtgrpIntcode(), equmtgrpInfoList.get(i).getEqumtgrpMsfcode());
//    	   		}
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
} 
